package com.hnucm.cooklife.custom;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.cooklife.R;

public class UsageItemDecoration extends RecyclerView.ItemDecoration {

    private Paint mPaint;
    private Path mPath;

    @SuppressLint("ResourceAsColor")
    public UsageItemDecoration() {
        mPaint = new Paint();
        mPaint.setColor(R.color.rgray);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(4);
        mPath = new Path();
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        RecyclerView.LayoutManager manager = parent.getLayoutManager();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            mPaint.setPathEffect(new DashPathEffect(new float[]{5,5},20));
            assert manager != null;
            int bottom = manager.getBottomDecorationHeight(child);
            int top = manager.getTopDecorationHeight(child);

            mPath.moveTo(child.getLeft() + 60,child.getBottom());
            mPath.lineTo(child.getRight() - 60,child.getBottom());

//            c.drawLine(0,child.getBottom(),child.getRight(),child.getBottom(),mPaint);
            c.drawPath(mPath,mPaint);
        }
    }

    /**
     *
     * @param outRect
     * 指item被撑开的距离，有上下左右四个值，默认为0
     */
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = 10;
    }
}
